package com.google.gwt.sample.stockwatcher.server;

import com.google.gwt.sample.stockwatcher.client.StockPrice;
import com.google.gwt.sample.stockwatcher.client.StockPriceService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class StockPriceServiceImpl extends RemoteServiceServlet implements StockPriceService{
	public StockPrice[] getPrices(StockPrice[] symbols){
		
		ResultSet rs = null; 
		ResultSet rss = null; 
		Statement statement = null;
		Connection con = null;
		int count=0;
		try {
			con = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5433/postgres", "postgres", "hr");
			statement = con.createStatement();
			rss = statement.executeQuery("SELECT count(id) FROM manual");
			while(rss.next()){	
					count=rss.getInt(1);
			}
			rss.close();
			statement.close();
		    con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			
		StockPrice[] prices = new StockPrice[count];
		
			rs = null; 
			statement = null;
			con = null;
			try {
			con = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5433/postgres", "postgres", "hr");
			statement = con.createStatement();
			rs = statement.executeQuery("SELECT * FROM \"manual\" ");
			int i=0;
			while(rs.next()){
					prices[i] = new StockPrice(rs.getInt("id"),rs.getString("name"), rs.getString("surname"), rs.getString("phone"));
					i++;
			}
			rs.close();
			statement.close();
		    con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		return prices;
		
	}
}